import math,requests
import time,execjs

def js_code():
    with open('./小米应用.js','r',encoding='utf-8') as f:
        js_file = execjs.compile(f.read()).call('params')
    return  js_file

def getToken():
    url = "https://verify.sec.xiaomi.com/captcha/v2/data"
    params = {
        "k": "c3ac4b9db2c84bc5a06c195bca5eabff",
        "locale": "zh_CN",
        "_t": str(math.ceil(time.time() * 1000))
    }
    data = dict({
        "a": "search"
    },**js_code())
    response = requests.post(url, headers=headers, params=params, data=data)

    return response.json()['data']['token']

def get_content():
    cookies = {
        "t_id": "noimeiweb_3fa8f887-89bd-4c52-a4d2-e97baea0b137",
        "__utmc": "127562001",
        "__utmz": "127562001.1697105532.1.1.utmcsr=cn.bing.com^|utmccn=(referral)^|utmcmd=referral^|utmcct=/",
        "Hm_lvt_765fefc2c357bae3970cea72e714811b": "1697105532",
        "__utma": "127562001.1195171056.1697105532.1697115847.1697118799.4",
        "Hm_lpvt_765fefc2c357bae3970cea72e714811b": "1697120749",
        "__utmb": "127562001.3.10.1697118799",
        "JSESSIONID": "9997BF0D8C4B0D113ADCD081C0E94772"
    }
    url = "https://app.mi.com/search"
    params = {
        "keywords": "金融",
        "token": getToken()
    }
    response = requests.get(url, headers=headers, cookies=cookies, params=params)

    print(response.text)
    print(response)

if __name__ == '__main__':
    headers = {
        "Accept": "*/*",
        "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Content-type": "application/x-www-form-urlencoded",
        "Origin": "https://app.mi.com",
        "Pragma": "no-cache",
        "Referer": "https://app.mi.com/",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "cross-site",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.47",
        "sec-ch-ua": "Microsoft",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "Windows"
    }
    get_content()